//
//  QSSignInRecordCell.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/23.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSSignInRecordCell.h"
#import <SDAutoLayout.h>

static NSString *defaultCell = @"DefaultCell";

@implementation QSSignInRecordCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configSubView];
     [self configCollectinoView];
}

- (void)configCollectinoView{
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
    flowLaout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //上下间距
    flowLaout.minimumLineSpacing = 5;
    //左右间距
    flowLaout.minimumInteritemSpacing = 5;
    flowLaout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView.pagingEnabled = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    [self.collectionView reloadData];
    self.collectionView.collectionViewLayout = flowLaout;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:defaultCell];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}
- (void)configSubView{
    self.backgroundColor = [UIColor clearColor];
    self.contentView.layer.cornerRadius = 8;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mas_leading).offset(kRealValue(0));
        make.trailing.equalTo(self.mas_trailing).offset(kRealValue(0));
        make.top.equalTo(self.mas_top).offset(kRealValue(10));
        make.bottom.equalTo(self.mas_bottom).offset(kRealValue(0));
    }];
    self.grayLine.backgroundColor = [UIColor QSColorGrayEAEAEA];
    self.userImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.userImageView.layer.cornerRadius = 20;
    self.userImageView.layer.masksToBounds = YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void)setModel:(QSSignRecordDetailModel *)model{
    _model = model;
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:_model.headPic]];
    self.nameLabel.text = _model.adminName;
    
    self.dateLabel.text = [NSString stringWithFormat:@"签到时间： %@", [NSDate getTimeFromTimestampWithStr:_model.esTime format:@"yyyy-MM-dd HH:mm"]];
    self.locationLabel.text = [NSString stringWithFormat:@"签到地点： %@", _model.esAdress];
    self.visitLabel.text = [NSString stringWithFormat:@"拜访企业： %@", _model.companyName];
    if (_model.esRemark.length > 0) {
        self.remarkLabel.text = [NSString stringWithFormat:@"签到备注： %@", _model.esRemark];        
    }else{
        self.remarkLabel.text = @"签到备注：";
    }
    
    CGFloat itemSize = kRealValue((kScreenWidth - 28 - 20 - 15) / 3);
    NSString *str = _model.esPic;
    if (str.length ==0) {
        self.collectionViewH.constant = itemSize * 0;
        [self setupAutoHeightWithBottomView:self.remarkLabel bottomMargin:20];
    }else{
    _picArr = [str componentsSeparatedByString:@","]; //字符串按照【分隔成数组
    if (_picArr.count > 0 && _picArr.count <= 3) {
        self.collectionViewH.constant = itemSize;
    }
    if (_picArr.count > 3 && _picArr.count <= 6) {
        self.collectionViewH.constant = itemSize * 2 + 5;
    }
    if (_picArr.count > 6 && _picArr.count <= 9) {
        self.collectionViewH.constant = itemSize * 3 + 10;
    }
        [self setupAutoHeightWithBottomView:self.collectionView bottomMargin:20];
    }
    [self.collectionView reloadData];
   
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _picArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:defaultCell forIndexPath:indexPath];
    for(UIView *subView in cell.contentView.subviews){
        if(subView){
            [subView removeFromSuperview];
        }
    }
    UIImageView *pic = [[UIImageView alloc] initWithFrame:cell.contentView.frame];
    pic.contentMode = UIViewContentModeScaleAspectFill;
    pic.layer.masksToBounds = YES;
    [pic sd_setImageWithURL:[NSURL URLWithString:_picArr[indexPath.item]]];
    [cell.contentView addSubview:pic];
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat itemSize = kRealValue((kScreenWidth - 28 - 20 - 15) / 3);
    return CGSizeMake(itemSize, itemSize);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSource = self;
    browser.currentPage = indexPath.item;
    //展示
    [browser show];
}
#pragma mark - YBDataSource
- (NSInteger)yb_numberOfCellsInImageBrowser:(YBImageBrowser *)imageBrowser{
    return _picArr.count;
}

- (id<YBIBDataProtocol>)yb_imageBrowser:(YBImageBrowser *)imageBrowser dataForCellAtIndex:(NSInteger)index{
    YBIBImageData *data = [YBIBImageData new];
        data.imageURL = [NSURL URLWithString:_picArr[index]];
    //    data.sourceObject = _pictureList[index].;
        return data;
}

////内边距处理
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
//    // UIEdgeInsets insets = {top, left, bottom, right};
//    return UIEdgeInsetsMake(0, 0, 0, 0);
//}

@end
